import os
from tortoise import Tortoise

from common import logger
from conf.config import Config
Models_DIR = os.path.join(Config.BASE_DIR, 'dal', 'sql_dal', 'models')


def get_models_list():
    _models_list = ["aerich.models"]
    for file in os.listdir(Models_DIR):
        if "__init__.py" == str(file) or '.py' not in str(file):
            continue
        file_name = str(file).replace('.py', '')
        _models_list.append(f'dal.sql_dal.models.{file_name}')
    return _models_list


TORTOISE_ORM = {
    "connections": {
        "default": Config.mysql.link["read"].get_database_url(),
        "write": Config.mysql.link["write"].get_database_url()
    },
    "apps": {
        "models": {
            "models": get_models_list(),
            "default_connection": "write",
        },
    },
    'use_tz': False,
    'timezone': 'Asia/Shanghai'
}


async def init_tortoise():
    await Tortoise.init(TORTOISE_ORM)
    logger.success("[Tortoise] 初始化 Tortoise 成功")
